//四个接口 验证手机号是否注册，发送验证码，验证验证码是否正确，注册
//1.点击创建帐户按钮时
var regbtn = document.getElementById("regbtn");

regbtn.onclick = function(){
    var telValue = tel.value;
    var pwdValue = pwd.value;
    var yzmValue = yzm.value;

    var telIsRight = false;
    var pwdIsRight = false;

    var telReg = /^1[3-9][0-9]{9}$/;

    if(telValue.length==0) {
        error1.innerHTML = "此项必须填写";
        telIsRight = false;
    }else if(!telReg.test(telValue)){
        error1.innerHTML = "手机号无效";
        telIsRight = false;
    }else{
        telIsRight = true;
    }

    if(pwdValue.length==0){
        error2.innerHTML = "此项必须填写";
        error2.className = "error";
        pwd.className = "";
        pwdIsRight = false;
    }else if(pwdValue.length<6){
        error2.innerHTML = "密码至少6位，支持数字、字母和符号";
        error2.className = "error";
        pwd.className = "";
        pwdIsRight = false;
    }else if(pwdValue.length<8){
        error2.innerHTML = "强度高的密码至少需要8个字符，同时包含大小写字母、数字和符号。";
        pwd.className = "warnbg";
        error2.className = "warn";
        pwdIsRight = true;
    }else{
        error2.innerHTML = "";
        error2.className = "error";
        pwd.className = "";
        pwdIsRight = true;
    }

   // alert(1);

    if(yzmValue.length==0){
        error3.innerHTML = "此项必须填写";
    }else{
        //Promise
        $.ajax({
            type:"POST",
            url:baseurl+"/bijiyzm/find2",
            contentType : "application/json; charset=utf-8", //会发一个用UTF-8编码的JSON的字符串给服务器
            data:JSON.stringify({ //data就是这个字符串
                tel:tel.value,
                yzm:yzm.value
            }),
            success:function(data){ //data返回给前端的东西
                if(!data){//如果data为true
                   error3.innerHTML = "验证码错误！";
                }else{
                    if(telIsRight&&pwdIsRight){
                        regbtn.getElementsByClassName("btnloading")[0].style.display = "block";

                        $.ajax({
                            type:"POST",
                            url:baseurl+"/bijiuser/add",
                            contentType : "application/json; charset=utf-8", //会发一个用UTF-8编码的JSON的字符串给服务器
                            data:JSON.stringify({
                                "createDate": new Date().toLocaleString(),
                                "pwd": pwd.value,
                                "tel": tel.value
                            }),
                            success:function(data){ //data返回给前端的东西
                                if(data){
                                    //tip.innerHTML = "注册成功！";
                                    alert("注册成功！")
                                    location = "login.html";
                                }
                            },
                            error:function(data){
                                tip.innerHTML = "发生错误！";
                            }
                        })
                    }
                }
            }
        })
    }


};

//2.当光标进入输入框时
tel.onfocus = function(){
    error1.innerHTML = "";
};

//当光标离开文本框时,验证手机号是否注册
tel.onblur = function(){
    var telReg = /^1[3-9][0-9]{9}$/;
    var telValue = tel.value;

    if(telValue.length==0) {
        error1.innerHTML = "此项必须填写";
        return;
    }else if(!telReg.test(telValue)){
        error1.innerHTML = "手机号无效";
        return;
    }

    $.ajax({
        type:"POST",
        url:baseurl+ "/bijiuser/checktel",
        contentType:"application/json;charset=utf-8",
        data:JSON.stringify({
            "tel": tel.value
        }),
        success:function(data){
            error1.innerHTML = data?"":"手机号已经注册！";
        }
    });
};

pwd.onfocus = function(){
    error2.innerHTML = "";
};

yzm.onfocus = function(){
    error3.innerHTML = "";
};

//3.获取验证码功能
getyzmbtn.onclick = function(){
    var telReg = /^1[3-9][0-9]{9}$/;
    var telValue = tel.value;

    if(telValue.length==0) {
        error1.innerHTML = "此项必须填写";
        return;
    }else if(!telReg.test(telValue)){
        error1.innerHTML = "手机号无效";
        return;
    }

    error1.innerHTML = "";

    sendMsg(tel.value,true);
};